package com.labichaoka.chaoka.ui.splash;

import android.widget.Toast;

import com.labichaoka.chaoka.base.MyApplication;
import com.labichaoka.chaoka.entity.BaseResponse;
import com.labichaoka.chaoka.entity.VersionCheckResponse;
import com.labichaoka.chaoka.net.BaseSubscriber;
import com.labichaoka.chaoka.net.DataManager;

import java.util.Map;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by ruan on 2018/6/4.
 */

public class SplashInteractorImpl implements SplashInteractor {
    private DataManager manager = new DataManager(MyApplication.mContext);
    private CompositeSubscription mCompositeSubscription = new CompositeSubscription();

    //检查更新
    @Override
    public void checkUpdate(String versionId, final OnCheckUpdateFinishedListener listener) {
        mCompositeSubscription.add(manager.checkVersion(versionId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<VersionCheckResponse>() {

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        listener.onFailed();
                    }

                    @Override
                    public void onNext(VersionCheckResponse response) {

                        if (response.getCode().equals("1")) {
                            //有升级
                            listener.onHasUpdate(response);

                        } else if (response.getCode().equals("200")) {
                            listener.onNoUpdate();
                        }
                    }
                })
        );
    }

    /*上传通讯录*/
    @Override
    public void uploadContactList(Map<String, String> paramsMap, OnCheckUpdateFinishedListener listener) {
        mCompositeSubscription.add(manager.uploadContactList(paramsMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<BaseResponse>() {

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        listener.onUploadContactListFailed();
                    }

                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        super.onNext(baseResponse);
                        listener.onUploadContactListSuccess(baseResponse);
                    }
                })
        );
    }
}
